package com.sample.library.service.persistence;

import java.util.List;

import org.springframework.jdbc.object.SqlQuery;

import com.liferay.portal.kernel.dao.orm.ORMException;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.util.dao.orm.CustomSQLUtil;
import com.sample.library.model.Author;
import com.sample.library.model.impl.AuthorImpl;

public class AuthorFinderImpl extends AuthorPersistenceImpl implements AuthorFinder{
	private final String FIND_BY_FULLNAME = AuthorFinder.class.getName() + ".searchByAuthorFullName";
	private final String COUNT_BY_FULLNAME = AuthorFinder.class.getName() + ".countByAuthorFullName";
	
	public List<Author> searchByAuthorFullName(String name) {
		Session session = null;
		try {
			session = openSession();
			
			//Lay cau lenh sql tu author.xml vao
			String sql = CustomSQLUtil.get(FIND_BY_FULLNAME);
			
			sql = CustomSQLUtil.replaceAndOperator(sql, true);
			System.out.println("sql: " + sql);
			
			SQLQuery query = session.createSQLQuery(sql);
			query.addEntity("Author", AuthorImpl.class);
			
			QueryPos qPos = QueryPos.getInstance(query);
			qPos.add( "%" + name + "%");
			qPos.add( name);
			qPos.add( "%" + name + "%");
			qPos.add( name);
			
			List list = QueryUtil.list(query, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
			return list;
		} catch (ORMException e) {
			
		}
		return null;
	}
	
	public int countByAuthorFullName(String name) {
		return 0;
	}
}
